import type { NodeItem } from './types'
// 发起流程
export const initFlow = () => {
  const node: NodeItem = createNode()
  node.name = '发起人'
  node.nodeId = 'start-node'
  node.type = 0
  return node
}

// 创建条件分支节点
export const createConditionNode = (prevId: string) => {
  const num = 2 // 新建分支数
  const arr: NodeItem[] = []
  for (let i = 1; i <= num; i++) {
    const node: NodeItem = createNode()
    node.type = 3
    node.name = '条件' + i
    node.nodeId = 'condition-' + i + new Date().getTime()
    node.prevId = prevId
    arr.push(node)
  }
  return arr
}

// 创建节点
export const createNode = () => {
  const node: NodeItem = {
    type: 0,
    name: '',
    nodeId: '',
    prevId: '',
    properties: {
      actioners: [],
      
      approvalMethod: 1, // 审批方式 1会签 2或签
      approvalNone: 1, // 审批人为空时 1自动通过 2自动转交管理员
      canOver: 1, // 审批人可否选择继续或完结流程  1否 2是
      actionerRules: [],

      conditions: [],
      formData: []
    },
    childNode: null
  }
  return node
}

// 获取列表
export const getList = (rootNode: NodeItem) => {
  let arr = getObject(rootNode, [])
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].type === 4) {
      for (let j = 0; j < arr[i].conditionNodes.length; j++) {
        const branchObject = arr[i].conditionNodes[j]
        const branchList = getObject(branchObject, [])
        arr = arr.concat(branchList)
      }
    }
  }
  return arr
}

// 获取对象属性
export const getObject: (obj: any, arr: NodeItem[]) => any = (obj, arr) => {
  arr.push(obj)
  if (obj.childNode) {
    return getObject(obj.childNode, arr)
  } else {
    return arr
  }
}

// 解析列表为对象
export const analysis = (arr: NodeItem[]) => {
  const result = []
  for (let i = 0; i < arr.length; i++) {
    const node = arr[i]
    result.push(findParentNode(arr, node))
  }
  return result.find((el) => el.nodeId === 'start-node')
}

// 查找父节点
export const findParentNode = (arr: NodeItem[], node: NodeItem) => {
  const parent = arr.find((el) => el.nodeId === node.prevId)
  if (parent) {
    if (parent.type === 4 && node.type === 3) {
      parent.conditionNodes = parent.conditionNodes?.map((el) => {
        if (el.nodeId === node.nodeId) {
          return node
        } else {
          return el
        }
      })
    } else {
      parent.childNode = node
    }
    return parent
  } else {
    return node
  }
}

// 移除节点
export const removeNode = (list: NodeItem[], node: NodeItem) => {
  let arr = []
  const parentNode = list.find((el) => {
    return el.nodeId === node.prevId
  })
  arr = list.filter((el) => el.nodeId != node.nodeId)
  // 条件
  if (node.type === 3) {
    // 2个条件 移除一个，则路由&条件全部移除
    if (parentNode && parentNode.conditionNodes && parentNode.conditionNodes.length < 3) {
      for (let i = 0; i < parentNode.conditionNodes.length; i++) {
        const nodeId = parentNode.conditionNodes[i].nodeId
        arr = arr.filter((el) => el.nodeId != nodeId)
      }
      arr = arr.filter((el) => el.nodeId != parentNode.nodeId)
      // 元素子节点挂载到父节点上
      const parentParentNode = arr.find((el) => {
        return el.nodeId === parentNode.prevId
      })
      if (parentNode.childNode) parentNode.childNode.prevId = parentNode.prevId
      if (parentParentNode && parentParentNode.childNode) {
        arr = arr.map((el) => {
          if (el.nodeId === parentNode.prevId) {
            el.childNode = parentNode.childNode
          }
          return el
        })
      }
    } else {
      arr = arr.map((el) => {
        if (el.nodeId === node.prevId) {
          el.conditionNodes = el.conditionNodes?.filter((el) => el.nodeId != node.nodeId)
        }
        return el
      })
    }
  } else {
    // 元素子节点挂载到父节点上
    if (node.childNode) node.childNode.prevId = node.prevId
    if (parentNode && parentNode.childNode) {
      arr = arr.map((el) => {
        if (el.nodeId === node.prevId) {
          el.childNode = node.childNode
        }
        return el
      })
    }
  }
  return arr
}
